/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank.models;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author shengchang
 */
public class Map {
    private Image backgroundImage;
    private Vector<Item> itemList; 
    
    public Map()
    {
        itemList = new Vector<Item>();
    }

    public void setBackgroundImage(Image backgroundImage) {
        this.backgroundImage = backgroundImage;
    }
    
    public void setBackgroundImage(String mapBackgroundTypeFile)
    {
        try {
            this.backgroundImage = ImageIO.read(new File(mapBackgroundTypeFile));
        } catch (IOException ex) {
            Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setItemList(Vector<Item> itemList) {
        this.itemList = itemList;
    }
    
    public void setItemList(String mapLevelFile)
    {
        try {
            FileInputStream fis = null;
            File f = new File(mapLevelFile);
            fis = new FileInputStream(f);
            int c = -1;
            int x = 0;
            int y = 0;
            while( (c = fis.read()) != -1 ){
                if( c == '\n'){
                    y++;
                    x = 0;
                }
                else if(c == '#')
                {
                    Item item = new Item();
                    item.setImage(Global.enemyTankTypeVector.get(Global.enemyTankType));
                    item.setWidth(50);
                    item.setHeight(50);
                    item.setX(x*50);
                    item.setY(y*50);
                    itemList.add(item);
                    x++;
                }
                else if(c == 'W')
                {
                    Item item = new Item();
                    item.setImage(Global.wallTypeVector.get(Global.wallType));
                    item.setWidth(50);
                    item.setHeight(50);
                    item.setX(x*50);
                    item.setY(y*50);
                    itemList.add(item);
                    x++;
                } else {
                    x++;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Image getBackGroundImage() {
        return backgroundImage;
    }

    public Vector<Item> getItemList() {
        return itemList;
    }
}
